package com.lsx.logisticscore.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lsx.logistics.AssertUtil;
import com.lsx.logistics.response.CommonResp;
import com.lsx.logisticscore.request.GetTicketInfoVo;
import com.lsx.logisticscore.entity.Ticket;
import com.lsx.logisticscore.mapper.LogMapper;
import com.lsx.logisticscore.mapper.TicketMapper;
import com.lsx.logisticscore.response.ResponseData;
import com.lsx.logisticscore.response.TicketResp;
import com.lsx.logisticscore.service.LogService;
import com.lsx.logisticscore.service.TicketService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/ticket")
public class TicketController {

    @Autowired
    private TicketService ticketService;

    @Autowired
    private TicketMapper ticketMapper;

    @Autowired
    private LogMapper logMapper;

    @Autowired
    private LogService logService;

    @RequestMapping("/add")
    public CommonResp<ResponseData> addTicket(@RequestBody String params){
        GetTicketInfoVo ticketInfo = JSONObject.parseObject(params, GetTicketInfoVo.class);
        int result = ticketService.insertTicket(ticketInfo);
        CommonResp<ResponseData> resp = new CommonResp<>();
        ResponseData data = new ResponseData();
        if (result == -1){
            data.setCode("-1");
            data.setMessage("该产品不存在！");
            resp.setContent(data);
            return resp;
        }else if (result == -2){
            data.setCode("-2");
            data.setMessage("产品库存不足！");
            resp.setContent(data);
            return resp;
        }else if (result == -3){
            data.setCode("-3");
            data.setMessage("负责人不存在！");
            resp.setContent(data);
        }
        data.setMessage(String.valueOf(result));
        resp.setContent(data);
        return resp;
    }

    @RequestMapping("/remove")
    public CommonResp<ResponseData> removeTicket(@RequestBody JSONObject param){
        String ticketId = param.getString("ticketId");
        int result = ticketService.deleteTicket(Integer.valueOf(ticketId));
        CommonResp<ResponseData> resp = new CommonResp<>();
        if (result < 1){
            resp.setContent(new ResponseData("-1", "订单正在进行中，无法删除！"));
            return resp;
        }
       resp.setContent(new ResponseData());
        return resp;
    }

    @RequestMapping("/{ticketId}")
    public CommonResp<Ticket> getTicketById(@PathVariable Integer ticketId){
        return new CommonResp<>(ticketMapper.selectById(ticketId));
    }

    @RequestMapping("/update")
    public CommonResp<ResponseData> updateTicketById(@RequestBody JSONObject params){
        GetTicketInfoVo ticketInfo = params.toJavaObject(GetTicketInfoVo.class);
        String ticketId = params.getString("ticketId");
        ticketService.updateTicket(ticketInfo);
        return new CommonResp<>(new ResponseData());
    }

    @RequestMapping("/getCountIn7day")
    public CommonResp<List<List<Map<String, Object>>>> getCountIn7day(){
        List<Map<String, Object>> map1 = ticketMapper.selectNewCountIn7day();
        List<Map<String, Object>> map2 = ticketMapper.selectFinishCountIn7day();
        List<List<Map<String, Object>>> list = new ArrayList<>();
        list.add(map1);
        list.add(map2);
        return new CommonResp<>(list);
    }

    @RequestMapping("/getSumIn7day")
    public CommonResp<List<Map<String, Object>>> getSumIn7day(){
        return new CommonResp<>(ticketMapper.selectSumIn7day());
    }
//    @RequestMapping("/getByEmpId")
//    public CommonResp<Page<Map<String, Object>>> getTicketByEmpId(Page<Map<String, Object>> page, String empId){
//        return new CommonResp<>(ticketMapper.selectTicketInfoByEmpId(page, empId));
//    }

//    @RequestMapping("/getLatestState")
//    public String getLatestState(Integer ticketId){
//        return logMapper.getLatestLog(ticketId).getStatus();
//    }

    @RequestMapping("/list")
    public CommonResp<Page<TicketResp>> listTicket(Page<TicketResp> page, GetTicketInfoVo params){
        return new CommonResp<>(ticketMapper.selectTicketInfo(page, params));
    }

    @RequestMapping("/getCountByStatus")
    public CommonResp<List<Map<String, Object>>> getCountByStatus(){
        return new CommonResp<>(ticketMapper.selectCountByStatus());
    }
}
